import 'api_response.dart';

/// 告警日志模型
class AlertLog {
  final int? logId;           // 日志ID 
  final int? alertId;         // 告警规则ID
  final int? deviceId;        // 设备ID
  final int? productId;       // 产品ID
  final String? alertName;    // 告警名称
  final String? propertyIdentifier; // 监控的属性标识符
  final String? propertyValue;     // 属性值
  final int? level;           // 告警级别（1:信息 2:警告 3:严重 4:紧急）
  final int? status;          // 告警状态（0:产生 1:恢复 2:已确认）
  final String? content;      // 告警内容
  final String? alertTime;    // 告警时间
  final String? recoverTime;  // 恢复时间
  final String? confirmTime;  // 确认时间
  final String? confirmUser;  // 确认用户
  final String? createBy;     // 创建者
  final String? tenantId;     // 租户ID
  
  // 设备和产品信息（用于显示）
  final String? deviceName;   // 设备名称
  final String? productName;  // 产品名称
  final String? deviceSn;     // 设备序列号
  final String? productKey;   // 产品标识码

  AlertLog({
    this.logId,
    this.alertId,
    this.deviceId,
    this.productId,
    this.alertName,
    this.propertyIdentifier,
    this.propertyValue,
    this.level,
    this.status,
    this.content,
    this.alertTime,
    this.recoverTime,
    this.confirmTime,
    this.confirmUser,
    this.createBy,
    this.tenantId,
    this.deviceName,
    this.productName,
    this.deviceSn,
    this.productKey,
  });

  /// 从JSON构造对象
  factory AlertLog.fromJson(Map<String, dynamic> json) {
    return AlertLog(
      logId: json['logId'],
      alertId: json['alertId'],
      deviceId: json['deviceId'],
      productId: json['productId'],
      alertName: json['alertName'],
      propertyIdentifier: json['propertyIdentifier'],
      propertyValue: json['propertyValue'],
      level: json['level'],
      status: json['status'],
      content: json['content'],
      alertTime: json['alertTime'],
      recoverTime: json['recoverTime'],
      confirmTime: json['confirmTime'],
      confirmUser: json['confirmUser'],
      createBy: json['createBy'],
      tenantId: json['tenantId'],
      deviceName: json['deviceName'],
      productName: json['productName'],
      deviceSn: json['deviceSn'],
      productKey: json['productKey'],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'logId': logId,
      'alertId': alertId,
      'deviceId': deviceId,
      'productId': productId,
      'alertName': alertName,
      'propertyIdentifier': propertyIdentifier,
      'propertyValue': propertyValue,
      'level': level,
      'status': status,
      'content': content,
      'alertTime': alertTime,
      'recoverTime': recoverTime,
      'confirmTime': confirmTime,
      'confirmUser': confirmUser,
      'createBy': createBy,
      'tenantId': tenantId,
      'deviceName': deviceName,
      'productName': productName,
      'deviceSn': deviceSn,
      'productKey': productKey,
    };
  }
  
  @override
  String toString() {
    return 'AlertLog(logId: $logId, alertName: $alertName, status: $status, level: $level)';
  }
  
  /// 创建此对象的拷贝，但用新值替换指定字段
  AlertLog copyWith({
    int? logId,
    int? alertId,
    int? deviceId,
    int? productId, 
    String? alertName,
    String? propertyIdentifier,
    String? propertyValue,
    int? level,
    int? status,
    String? content,
    String? alertTime,
    String? recoverTime,
    String? confirmTime,
    String? confirmUser,
    String? deviceName,
    String? deviceSn,
    String? productName,
  }) {
    return AlertLog(
      logId: logId ?? this.logId,
      alertId: alertId ?? this.alertId,
      deviceId: deviceId ?? this.deviceId,
      productId: productId ?? this.productId,
      alertName: alertName ?? this.alertName,
      propertyIdentifier: propertyIdentifier ?? this.propertyIdentifier,
      propertyValue: propertyValue ?? this.propertyValue,
      level: level ?? this.level,
      status: status ?? this.status,
      content: content ?? this.content,
      alertTime: alertTime ?? this.alertTime,
      recoverTime: recoverTime ?? this.recoverTime,
      confirmTime: confirmTime ?? this.confirmTime,
      confirmUser: confirmUser ?? this.confirmUser,
      deviceName: deviceName ?? this.deviceName,
      deviceSn: deviceSn ?? this.deviceSn,
      productName: productName ?? this.productName,
    );
  }
  
  /// 告警级别描述
  String getLevelDesc() {
    switch (level) {
      case 1:
        return '信息';
      case 2:
        return '警告';
      case 3:
        return '严重';
      case 4:
        return '紧急';
      default:
        return '未知';
    }
  }
  
  /// 告警状态描述
  String getStatusDesc() {
    switch (status) {
      case 0:
        return '产生';
      case 1:
        return '恢复';
      case 2:
        return '已确认';
      default:
        return '未知';
    }
  }
}

/// 告警日志列表响应
class AlertLogListResponse extends ApiResponse {
  final List<AlertLog> rows;
  final int total;

  AlertLogListResponse({
    required int code,
    required String msg,
    required this.rows, 
    required this.total,
  }) : super(code: code, msg: msg);

  factory AlertLogListResponse.fromJson(Map<String, dynamic> json) {
    List<AlertLog> rows = [];
    if (json['rows'] != null) {
      rows = (json['rows'] as List).map((item) => AlertLog.fromJson(item)).toList();
    }
    
    return AlertLogListResponse(
      code: json['code'] ?? 500,
      msg: json['msg'] ?? '',
      rows: rows,
      total: json['total'] ?? 0,
    );
  }
}

/// 告警日志详情响应
class AlertLogDetailResponse extends ApiResponse {
  @override
  final AlertLog? data;

  AlertLogDetailResponse({
    required int code,
    required String msg,
    this.data,
  }) : super(code: code, msg: msg);

  factory AlertLogDetailResponse.fromJson(Map<String, dynamic> json) {
    return AlertLogDetailResponse(
      code: json['code'] ?? 500,
      msg: json['msg'] ?? '',
      data: json['data'] != null ? AlertLog.fromJson(json['data']) : null,
    );
  }
} 